﻿@using System.Data
@{
    Layout = "~/Areas/Admin/Views/Shared/AdminLayout.cshtml";
    ViewBag.Title = "RoleAlloat";
    DataSet table = ViewData["menudata"] as DataSet;
}

<style>
    *{overflow: auto;}
</style>
<div class="searchpanel">
  <p style="font-size: 12px; line-height: 50px;"> 请选择功能菜单！</p>
</div>
<table class="datatable">
    <tr>
        <th width="30">选择<input type="checkbox" id="checkall" /></th>
        <th width="100">父级菜单</th>
        <th width="100">子菜单</th>
        <th width="200">跳转地址</th>
        <th width="100">创建时间</th>
        <th width="300">备注</th>
        <th ></th>
    </tr>
    <tbody id="list">
        @{
            foreach (DataRow row in table.Tables[0].Rows)
            {
                <tr>
                    <td><input type="checkbox" class="ckp" id="@row["Id"]_p" var="@row["Id"]" value="@row["Id"]" @(row["rid"].ToString() == "" ? "" : "checked='checked'")  /></td>
                    <td>@row["Name"]</td>
                    <td>--</td>
                    <td>@row["Url"]</td>
                    <td>@Convert.ToDateTime(row["Date"]).ToString("yyyy-MM-dd")</td>
                    <td>@row["Remark"]</td>
                    <td></td>
                </tr>
                foreach (DataRow row1 in table.Tables[1].Rows)
                {
                    if (row["Id"].ToString() == row1["Pid"].ToString())
                    {
                        <tr>
                            <td><input type="checkbox" class="@row1["Pid"]_c" var="@row1["Id"]" value="@row1["Id"]" @(row1["rid"].ToString() == "" ? "" : "checked='checked'") /></td>
                            <td>-</td>
                            <td>@row1["Name"]</td>
                            <td>@row1["Url"]</td>
                            <td>@Convert.ToDateTime(row1["Date"]).ToString("yyyy-MM-dd")</td>
                            <td>@row1["Remark"]</td>
                            <td></td>
                        </tr>
                    }
                }
              
            }

        }
       
    </tbody>
</table>
<div style="width: 250px;height: 50px;margin-top: 50px;margin-bottom: 50px;">
    <div class="botton" onclick="edit()">保存</div>
    <div class="botton"onclick="javascript: history.go(-1);">取消</div>
</div>
<script>
    //选择全部
    $("#checkall").click(function() {
        if ($("#checkall").attr("checked")) {
            $("#list input[type=checkbox]").attr("checked", "checked");
        } else {
            $("#list input[type=checkbox]").removeAttr("checked");
        }
    });
    $("input:checkbox").click(function () {
        var t = $(this).attr("class").split('_');
        if (t[1] == "c") {
            $("#" + t[0] + "_p").attr("checked","true");
        }
    });
    $(".ckp").click(function() {
        var id = $(this).attr("id").split("_")[0];
        if ($(this).attr("checked")) {
            $("." + id + "_c").attr("checked", "true");
        } else {
            $("." + id + "_c").removeAttr("checked");
        }
    });

    function edit() {
        var ids = "";
        $("#list input[type=checkbox]").each(function () {
            if ($(this).is(":checked")) {
                ids+=$(this).attr("var")+",";
            }
        });

        ids = ids.substring(0, ids.length - 1);
        $.post("@Url.Action("FpRole")", { rid: "@ViewData["id"]", menuids: ids }, function (row) {
           if (row > 0) {
               alert("分配成功！");
           } else {
               alert("分配失败！");
           }
        });
    }
</script>